using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Windows.Forms;
using PrintDataGrid;
using System.Collections;
using System.Collections.Specialized;

namespace eBirdAttend
{
    public partial class INOUTStatus : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();

        public INOUTStatus()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
            sqlCmd.Connection = sqlCon;
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string strWhere = "";
                string strStartDate = dateToShow.Value.Date.ToString("MM/dd/yyyy");

                strWhere = strWhere + "Convert(varchar,trsTimeStamp,101)= convert(varchar, '" + strStartDate + "',101) ";

                if (cmbBranch.SelectedIndex != 0)
                {
                    strWhere = strWhere + " and EmpBrID = " + cmbBranch.SelectedValue.ToString();
                }

                if (cmbDept.SelectedIndex != 0)
                {
                    strWhere = strWhere + " and EmpDeptID = " + cmbDept.SelectedValue.ToString();
                }
                if (txtFirstName.Text != "")
                {
                    strWhere = strWhere + " and EmpFirstName like '%" + txtFirstName.Text + "%'";
                }
                if (txtLastName.Text != "")
                {
                    strWhere = strWhere + " and EmpLastName like '%" + txtLastName.Text + "%'";
                }
                //MessageBox.Show(strWhere.ToString());
                if (sqlCon.State == ConnectionState.Closed)
                sqlCon.Open();
                sqlCmd.CommandText = "SELECT     EmpFirstName + ' ' + EmpLastName as Name, trsTimeStamp as Time,INOUTStr as INOUT, DepName as Department, BrName as Branch, EmpId as EmpID "
                                      + "FROM         dbo.InoutStatusView ";


                if (strWhere != "")
                {
                    strWhere = " where " + strWhere;
                    sqlCmd.CommandText += strWhere;
                    sqlCmd.CommandText += " order by EmpId";
                }


                // MessageBox.Show(sqlCmd.CommandText);
                sqlCmd.CommandType = CommandType.Text;
               

                sqlCmd.ExecuteNonQuery();
                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCmd);
                sqlAdap.Fill(ds);
                sqlCmd.Parameters.Clear();
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
                dataGridView1.Columns[5].Visible = false;
                dataGridView1.Columns[0].MinimumWidth = 140;
                dataGridView1.Columns[1].MinimumWidth = 150;
                dataGridView1.Columns[2].MinimumWidth = 110;
                dataGridView1.Columns[3].MinimumWidth = 125;
                dataGridView1.Columns[4].MinimumWidth = 120;
                sqlCon.Close();
            }
            catch (Exception ex)
            {
            }

            /*sqlCmd.Parameters.Clear();
            sqlCmd.CommandText = "INOUTStatus";
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCon.Open();
            //sqlCmd.Parameters.Add("@brid", "1");
            //sqlCmd.Parameters.Add("@deptid", "1");
            SqlParameter param = sqlCmd.Parameters.AddWithValue("@date", System.DateTime.Today.ToString("MM/dd/yyyy"));
            if (cmbBranch.SelectedIndex != 0)
            {
                sqlCmd.Parameters.AddWithValue("@brid", cmbBranch.SelectedValue);
            }
            if (cmbDept.SelectedIndex != 0)
            {
                sqlCmd.Parameters.AddWithValue("@deptid", cmbDept.SelectedValue);
            }
            if (txtEmpid.Text != "")
            {
                sqlCmd.Parameters.AddWithValue("@empcode", txtEmpid.Text);
            }
            if (txtLastName.Text != "")
            {
                sqlCmd.Parameters.AddWithValue("@lastname", txtLastName.Text);
            }
            if (txtFirstName.Text != "")
            {
                sqlCmd.Parameters.AddWithValue("@firstname", txtFirstName.Text);
            }
            sqlCmd.Parameters.AddWithValue("@groupby", cmbGroup.SelectedIndex);

            //sqlCmd.Param
            SqlDataReader sqlReader;
            sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                sqlReader.Close();
                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCmd);
                sqlAdap.Fill(ds);
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
            }
            sqlCon.Close();*/
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void INOUTStatus_Load(object sender, EventArgs e)
        {
            cmbGroup.SelectedIndex = 0;
            departmentDataset.Clear();
            departmentTableAdapter1.Fill(departmentDataset.Department);

            DataTable table1 = new DataTable("Table1");

            table1 = departmentDataset.Tables[0].Clone();

            // Add some rows.
            DataRow row;
            row = table1.NewRow();
            row["depid"] = 0;
            row["depname"] = "[Select]";
            row["depcmpid"] = "1";
            row["dephod"] = "Select";
            table1.Rows.Add(row);

            // Accept changes.
            table1.AcceptChanges();

            departmentDataset.Tables[0].Constraints.Clear();
            DataTable table2 = departmentDataset.Tables[0];

            table1.Merge(table2);

            cmbDept.DataSource = table1;
            cmbDept.DisplayMember = "depname";
            cmbDept.ValueMember = "depid";

            branchTableAdapter1.Fill(branchDataset.Branch);
            table1 = new DataTable("Table1");

            table1 = branchDataset.Tables[0].Clone();

            // Add some rows.
            row = table1.NewRow();
            row["brbranchid"] = 0;
            row["brcmpid"] = 0;
            row["brname"] = "[Select]";

            table1.Rows.Add(row);

            // Accept changes.
            table1.AcceptChanges();

            branchDataset.Tables[0].Constraints.Clear();
            table2 = branchDataset.Tables[0];
            table1.Merge(table2);

            cmbBranch.DataSource = table1;
            cmbBranch.DisplayMember = "brname";
            cmbBranch.ValueMember = "brbranchid";

            cmbDept.SelectedIndex = 0;
            cmbBranch.SelectedIndex = 0;
            dateToShow.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDGV printdgv = new PrintDGV();
            printdgv.Print_DataGridView(dataGridView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int j = 1;
            int i;
            //MessageBox.Show(Application.StartupPath.ToString());
            //MessageBox.Show(Application.CommonAppDataPath.ToString());
            string strReportTemplate = Application.StartupPath.ToString() + "\\" +
                Properties.Settings.Default.InOutReport.ToString();
            //MessageBox.Show(strReportTemplate);
            if (strReportTemplate == "")
            {
                MessageBox.Show("Report Template Name Missing");
                return;
            }

            //ConfigurationManager.AppSettings["AttendanceReport"].ToString();
            //ConfigurationSettings.AppSettings["AttendanceReport"].ToString();

            StringCollection strHeaderCol = new StringCollection();
            EbExcelAuto exelAttnReport = new EbExcelAuto();
            string curColText = "";
            exelAttnReport.Init();
            exelAttnReport.OpenExcelFile(strReportTemplate);
            exelAttnReport.FillExcellSheetFromDatagrid("#START", dataGridView1, "EmpID");
            exelAttnReport.SaveAs(null);
            exelAttnReport.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
